import requests
from bs4 import BeautifulSoup
import xlwt


def get_html(url):
    # 模拟浏览器访问
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.54',
        'accept-language': 'zh-CN,zh;q=0.9'
    }
    print("--> 正在获取网站信息")
    response = requests.get(url, headers=headers)  # 请求访问网站
    if response.status_code == 200:
        html = response.text  # 获取网页源码
        return html  # 返回网页源码
    else:
        print("获取网站信息失败！")


if __name__ == '__main__':
    excel = xlwt.Workbook(encoding='ascii')
    sheet = excel.add_sheet('test')
    sheet.write(0, 0, label='商品编号')
    sheet.write(0, 1, label='商品名称')
    sheet.write(0, 2, label='图片路径')
    sheet.write(0, 3, label='价格')
    sheet.write(0, 4, label='商家')
    sheet.write(0, 5, label='商品详情地址')
    row = 0

    # 搜索关键字
    keyword = '笔记本'
    # 搜索地址
    search_url = 'https://search.jd.com/Search?keyword=' + keyword + '&enc=utf-8&pvid=d0d0c6940f534142acf13afee3151bc5'
    html = get_html(search_url)
    soup = BeautifulSoup(html, 'lxml')
    goodsList = soup.find_all('li', class_='gl-item')
    for li in goodsList:  # 遍历父节点
        row += 1
        # 商品编号
        no = li['data-sku']
        # 商品名称
        name = li.find(class_='p-name p-name-type-2').find('em').get_text()
        # 图片路径
        img_url = li.find(class_='p-img').find('img')['data-lazy-img']
        if img_url is 'done':
            img_url = li.find(class_='p-img').find('img')['src']
        img_url = 'https:' + img_url
        # 价格
        price = li.find(class_='p-price').find('i').get_text()
        # 商家
        shop = li.find(class_='p-shop').find('a').get_text()
        # 商品详情地址
        detail_addr = li.find(class_='p-name p-name-type-2').find('a')['href']
        detail_addr = 'https:' + detail_addr

        sheet.write(row, 0, label=no)
        sheet.write(row, 1, label=name)
        sheet.write(row, 2, label=img_url)
        sheet.write(row, 3, label=price)
        sheet.write(row, 4, label=shop)
        sheet.write(row, 5, label=detail_addr)
    excel.save('./233.xls')
